package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.kitchen.KitchenSetMealInfo;
import org.dromara.kitchen.service.IKitchenSetMealInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 套餐里的美食信息
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/setMealInfo")
public class KitchenSetMealInfoController extends BaseController {

    private final IKitchenSetMealInfoService kitchenSetMealInfoService;

    /**
     * 查询套餐里的美食信息列表
     */
    @SaCheckPermission("kitchen:setMealInfo:list")
    @GetMapping("/list")
    public TableDataInfo<KitchenSetMealInfo> list(KitchenSetMealInfo kitchenSetMealInfo, PageQuery pageQuery) {
        QueryWrapper<KitchenSetMealInfo> qw = new QueryWrapper<>();

        TableDataInfo<KitchenSetMealInfo> td = PageUtils.buildDataInfo(kitchenSetMealInfoService.page(PageUtils.buildPage(),qw));
        List<KitchenSetMealInfo> list = td.getRows();
        return td;
    }

    /**
     * 导出套餐里的美食信息列表
     */
    @SaCheckPermission("kitchen:setMealInfo:export")
    @Log(title = "套餐里的美食信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KitchenSetMealInfo kitchenSetMealInfo, HttpServletResponse response) {
        List<KitchenSetMealInfo> list = kitchenSetMealInfoService.list();
        ExcelUtil.exportExcel(list, "套餐里的美食信息", KitchenSetMealInfo.class, response);
    }

    /**
     * 获取套餐里的美食信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:setMealInfo:query")
    @GetMapping("/{id}")
    public R<KitchenSetMealInfo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(kitchenSetMealInfoService.getById(id));
    }

    /**
     * 新增套餐里的美食信息
     */
    @SaCheckPermission("kitchen:setMealInfo:add")
    @Log(title = "套餐里的美食信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KitchenSetMealInfo kitchenSetMealInfo) {
        return toAjax(kitchenSetMealInfoService.save(kitchenSetMealInfo));
    }

    /**
     * 修改套餐里的美食信息
     */
    @SaCheckPermission("kitchen:setMealInfo:edit")
    @Log(title = "套餐里的美食信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KitchenSetMealInfo kitchenSetMealInfo) {
        return toAjax(kitchenSetMealInfoService.updateById(kitchenSetMealInfo));
    }

    /**
     * 删除套餐里的美食信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:setMealInfo:remove")
    @Log(title = "套餐里的美食信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(kitchenSetMealInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * 删除套餐里的美食信息
     *
     * @param kitchenSetMealInfo 主键串
     */
    @SaCheckPermission("kitchen:setMealInfo:remove")
    @Log(title = "套餐里的美食信息", businessType = BusinessType.DELETE)
    @PostMapping("/removeByInfo")
    public R<Void> removeByInfo(@RequestBody KitchenSetMealInfo kitchenSetMealInfo) {
        QueryWrapper<KitchenSetMealInfo> qw = new QueryWrapper<>();
        qw.eq("meal_id", kitchenSetMealInfo.getMealId());
        qw.eq("food_id", kitchenSetMealInfo.getFoodId());
        qw.last("limit 1");
        KitchenSetMealInfo info =  kitchenSetMealInfoService.getOne(qw);
        if(info != null){
            return toAjax(kitchenSetMealInfoService.removeById(info.getId()) ? 1 : 0);
        }
        return toAjax(0);
    }
}
